package com.yzz.hzraisedust.hzraseidust.main;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yzz.hzraisedust.hzraseidust.R;
import com.yzz.hzraisedust.hzraseidust.main.airquality.AirQualityDetailActivity;
import com.yzz.hzraisedust.hzraseidust.main.stastics.StasticTodayAdapter;
import com.yzz.hzraisedust.hzraseidust.modelbean.AirQualityBean;
import com.yzz.hzraisedust.hzraseidust.modelbean.DeviceCountBean;
import com.yzz.hzraisedust.hzraseidust.modelbean.RequstItemBean;
import com.yzz.hzraisedust.hzraseidust.modelbean.StasticItemBean;
import com.yzz.hzraisedust.hzraseidust.util.Constant;
import com.yzz.hzraisedust.hzraseidust.util.NetUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class StasticsFragment extends Fragment implements AdapterView.OnItemClickListener{
    public static final String TAG = MapInfoFragment.class.getName();
    private GridView gview;
    private static StasticsFragment Instance;
    private List<Map<String, Object>> data_list;
    private SimpleAdapter sim_adapter;
    // 图片封装为一个数组
    private int[] icon = { R.mipmap.stastic_work_place,
            R.mipmap.stastic_device_offline };
    private String[] iconName = { "工地", "设备离线" };

    private RequstItemBean<AirQualityBean> airQualaty;

    private ListView mListView;
    public StasticsFragment() {
    }
    public static StasticsFragment getInstance(){
        if(Instance == null){
            Instance = new StasticsFragment();
        }
        return Instance;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_stastics, container, false);
        gview = (GridView) view.findViewById(R.id.statics_count_gridview);
        //新建List
        data_list = new ArrayList<Map<String, Object>>();
        //获取数据
        getData();
        //新建适配器
        String [] from ={"image","text"};
        int [] to = {R.id.image,R.id.text};
        sim_adapter = new SimpleAdapter(getActivity(), data_list, R.layout.stastic_girdview_item_layout, from, to);
        //配置适配器
        gview.setAdapter(sim_adapter);

        mListView = view.findViewById(R.id.static_today_listview);

        DeviceCount deviceCount = new DeviceCount();
        AirQuality airQuality = new AirQuality();
        //请求数据
        NetUtil netUtil = new NetUtil(getActivity());
        Map<String,String> parms = new HashMap<>();
        parms.put(Constant.UID_LOWER_KEY,Constant.Uid);
        netUtil.requestData(deviceCount,deviceCount,Constant.DEVICE_COUNT_URL,parms);

        netUtil.requestData(airQuality,airQuality,Constant.AIR_QUALITY_STASTICS_URL,parms);
        return view;
    }
    public List<Map<String, Object>> getData(){
        //cion和iconName的长度是相同的，这里任选其一都可以
        for(int i=0;i<icon.length;i++){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", icon[i]);
            map.put("text", iconName[i]);
            data_list.add(map);
        }

        return data_list;
    }


    @Override
    public void onResume() {
        super.onResume();
        //((BaseActivity)getActivity()).showLoginingDlg();
        //NetUtil netUtil = new NetUtil(getActivity());
        //Map<String,String> param = new HashMap<>();
        //param.put(Constant.UID_KEY,Constant.Uid);

        //netUtil.requestData(this,null, Constant.SQUARE_URL,param);

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();

    }

    //更新空气质量列表
    private void initAirQuality(){
        StasticTodayAdapter stasticTodayAdapter = new StasticTodayAdapter(getActivity(),airQualaty.getRows());
        mListView.setAdapter(stasticTodayAdapter);
        mListView.setOnItemClickListener(this);
    }
    //更新工地总数及设备离线数目
    private void initDeviceCountGirdView(DeviceCountBean bean){
       String countAll = iconName[0]+":"+bean.getProjectNum();
       String deviceAll = iconName[1]+":"+bean.getDeviceNum();
        data_list.get(0).put("text",countAll);
        data_list.get(1).put("text",deviceAll);

        sim_adapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        AirQualityBean airQualityBean = airQualaty.getRows().get(i);
        Intent intent = new Intent(getActivity(), AirQualityDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constant.AIR_QUALITY_BEAN,airQualityBean);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    class DeviceCount implements Response.Listener<String>,Response.ErrorListener{

        @Override
        public void onErrorResponse(VolleyError volleyError) {

        }

        @Override
        public void onResponse(String s) {
            Gson gson = new Gson();
            DeviceCountBean countBean = gson.fromJson(s,DeviceCountBean.class);
            initDeviceCountGirdView(countBean);
        }
    }

    class AirQuality implements Response.Listener<String>,Response.ErrorListener{
        @Override
        public void onErrorResponse(VolleyError volleyError) {

        }

        @Override
        public void onResponse(String s) {
            Gson gson = new Gson();
            Type type = new TypeToken<RequstItemBean<AirQualityBean>>(){}.getType();
            airQualaty = gson.fromJson(s,type);
            if(airQualaty==null || !airQualaty.isSuccess()){
                Toast.makeText(getActivity(),"发生错误",Toast.LENGTH_LONG).show();
                return;
            }
            initAirQuality();
        }


    }

}

